package com.clss.commandhelper.function.homefragment1st;

import com.clss.commandhelper.base.BaseJson;
import com.clss.commandhelper.base.BasePresenter;
import com.clss.commandhelper.base.BaseView;
import com.clss.commandhelper.tools.Constant;
import com.clss.commandhelper.tools.eventbus.EventBusHelper;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 *
 * @author liuv
 * @date 2017/12/5
 */

public class Home1stPresenter extends BasePresenter {
    private static final String TAG = "Home2ndPresenter";

    private Home1stView mHome1stView;
    private Home1stModel mHome1stModel;
    private int mPage;
    private int mType;

    public void setHome4thView(Home1stView home1stView) {
        this.mHome1stView = home1stView;
    }

    public void getAlarmTaskList(){
        if (mHome1stModel == null){
            mHome1stModel = new Home1stModel(mHome1stView.getActContext());
        }
        mHome1stModel.get110AlarmTaskList(mType, mPage, new Callback<BaseJson>() {
            @Override
            public void onResponse(Call<BaseJson> call, Response<BaseJson> response) {
                mHome1stView.dismissLoadingDialog();
                BaseJson baseJson = response.body();
                EventBusHelper.handleBaseJson(baseJson);
                if (baseJson != null){
                    if (baseJson.getCode() != 0){
                        if (baseJson.getCode() == EventBusHelper.NO_MORE_DATA){
                            mHome1stView.get110AlarmTaskListSuccess(new ArrayList<Alarm110Entity>());
                        }
                        mHome1stView.responseError(baseJson.getCode(), baseJson.getError());
                    }else {
                        List<Alarm110Entity> alarm110List;
                        Type typeToken = new TypeToken<List<Alarm110Entity>>(){}.getType();
                        if (null == baseJson.getList()){
                            mHome1stView.responseError(baseJson.getCode(), baseJson.getError());
                        }else {
                            alarm110List = mGson.fromJson(baseJson.getList().toString(), typeToken);
                            mHome1stView.get110AlarmTaskListSuccess(alarm110List);
                        }
                    }
                }else {
                    mHome1stView.responseError(Constant.UNKNOW_ERROR_CODE, Constant.UNKNOW_ERROR_MESSAGE);
                }
            }

            @Override
            public void onFailure(Call<BaseJson> call, Throwable t) {
                mHome1stView.dismissLoadingDialog();
                mHome1stView.requestFailure(t);
            }
        });
    }

    @Override
    public void cancelCall() {

    }

    interface Home1stView extends BaseView{
        void get110AlarmTaskListSuccess(List<Alarm110Entity> alarmList);

    }
}
